package com.xxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.xxxx.server.pojo.Authority;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.Menuauthority;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAuthorityService;
import com.xxxx.server.service.IMenuService;
import com.xxxx.server.service.IMenuauthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LT
 * @since 2022-02-27
 */
@Api(value="权限管理",tags = "权限管理")
@RestController
@RequestMapping("")
public class AuthorityController {

    @Autowired
    private IAuthorityService authorityService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuauthorityService menuauthorityService;
    @ApiOperation(value = "获取所有权限")
    @GetMapping("/system/AuthorityManagement/get")
    public List<Authority> getAllAuthrity(){
        return  authorityService.list();
    }

    @ApiOperation(value = "添加权限")
    @PostMapping("/system/AuthorityManagement/add")
    public RespBean addAuthority(@RequestBody Authority authority) {
        if (authorityService.save(authority)) {
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "更新权限")
    @PutMapping("/system/AuthorityManagement/update")
    public RespBean updateAuthority(@RequestBody Authority authority)
    {
        if(authorityService.updateById(authority))
        {

            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败");
    }


    @ApiOperation(value = "删除权限")
    //@DeleteMapping("/delete/{id}")
    @DeleteMapping("/system/AuthorityManagement/delete")
    //    public RespBean deleteAuthority(@PathVariable Integer id)
    public RespBean deleteAuthority(@RequestParam("id") Integer id)
    {
        if (authorityService.removeById(id))
        {

            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/system/AuthorityManagement/menus")
    public List<Menu> getAllMenus(){
       return menuService.getAllMenus();
    }

    @ApiOperation(value = "根据角色ID查询菜单id")
    @GetMapping("/system/AuthorityManagement/menus/{id}")
    public List<Integer> getMenusById(@PathVariable Integer id){

        return menuauthorityService.list(new QueryWrapper<Menuauthority>()
                .eq("authorityId",id))
                .stream()
                .map(Menuauthority::getMenuId)
                .collect((Collectors.toList()));
    }

    @ApiOperation(value = "更新权限对应菜单")
    @PutMapping("/system/AuthorityManagement/updateMenuAuthority")
    public RespBean updateMenuAuthority(@RequestParam("authorityId")Integer authorityId, @RequestParam("menuId")List<Integer> menuId)
    {
        return menuauthorityService.updateMenuAuthority(authorityId,menuId);
    }



    }

